﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DDLManage.cs" company="HelloWorld">
//   BY黄媛媛
// </copyright>
// <summary>
//   Defines the updateDDLHandle type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Service
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;

    using WindowsFormsApp2.Model;

    /// <summary>
    /// The update deadline handle.
    /// </summary>
    public delegate void UpdateDDLHandle();

    /// <summary>
    /// The deadline manage.
    /// </summary>
    internal class DDLManage
    {
        /// <summary>
        /// The deadlines.
        /// </summary>
        [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Reviewed. Suppression is OK here.")]
        public List<Deadline> Deadlines = Deadline.Deadlines;

        /// <summary>
        /// The link.
        /// </summary>
        [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Reviewed. Suppression is OK here.")]
        public LinkDB Link = new LinkDB();

        /// <summary>
        /// Gets or sets the deadline data table.
        /// </summary>
        public DataTable DDLDataTable = new DataTable();

        /// <summary>
        /// Initializes a new instance of the <see cref="DDLManage"/> class.
        /// </summary>
        /// <param name="cId">
        /// The c id.
        /// </param>
        public DDLManage(string cId)
        {
            this.DDLDataTable.Columns.Add("任务内容");
            this.DDLDataTable.Columns.Add("截止日期");
            this.UpdateDDLInfo(cId);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DDLManage"/> class.
        /// </summary>
        public DDLManage()
        {
        }

        /// <summary>
        /// The update deadline.
        /// </summary>
        public static event UpdateDDLHandle UpdateDDL;

       

        /// <summary>
        /// The data initialization.
        /// </summary>
        public void DataInit()
        {
            this.Link.GetDeadLines(); // 从数据库读取ddl列表
            UpdateDDL?.Invoke();
        }

        /// <summary>
        /// The add deadline.
        /// </summary>
        /// <param name="weekday">
        /// The weekday.
        /// </param>
        /// <param name="chooseLesson">
        /// The choose lesson.
        /// </param>
        /// <param name="task">
        /// The task.
        /// </param>
        /// <param name="deadline">
        /// The deadline.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool AddDDL(int weekday, int chooseLesson, string task, string deadline)
        {
            long courseId;
            var query = from c in Course.Courses
                        where c.Weekday == weekday && c.BeginLesson <= chooseLesson && (c.LastLesson + c.BeginLesson) > chooseLesson
                        select c; // 查出courseID
            try
            {
                courseId = query.ElementAt(0).CourseId;
            }
            catch (Exception)
            {
                return false;
            }

            var newDDL = new Deadline(courseId, task, DateTime.Parse(deadline));
            this.Deadlines.Add(newDDL);
            this.Link.AddDeadline(newDDL); // 插入数据库

            UpdateDDL?.Invoke();
            return true;
        }

        /// <summary>
        /// The update deadline info.
        /// </summary>
        /// <param name="courseId">
        /// The course id.
        /// </param>
        public void UpdateDDLInfo(string courseId)
        {
            // 更新ddlDataTable
            var query = from ddl in this.Deadlines
                        where ddl.CourseId.ToString() == courseId
                        select ddl;

            foreach (var deadline in query.ToList())
            {
                var values = new object[2];
                values[0] = deadline.Task;
                values[1] = deadline.DeadlineDate.ToShortDateString();
                this.DDLDataTable.Rows.Add(values);
            }
        }

        /// <summary>
        /// The delete deadline.
        /// </summary>
        /// <param name="courseId">
        /// The course id.
        /// </param>
        /// <param name="ddlName">
        /// The deadline name.
        /// </param>
        public void DeleteDDL(string courseId, string ddlName)
        {
            this.Link.DeleteDeadline(courseId, ddlName);
            this.DDLDataTable.Clear();
            this.Deadlines.RemoveAll(ddl => ddl.CourseId.ToString() == courseId && ddl.Task == ddlName);
            this.UpdateDDLInfo(courseId);
            UpdateDDL?.Invoke();
        }

        /// <summary>
        /// The delete byCourse.
        /// </summary>
        /// <param name="courseId">
        /// The course id.
        /// </param>
        public void DeleteByCourse(string courseId)
        {
            this.Link.DeleteDDLByCourseId(courseId);
            this.Deadlines.RemoveAll(ddl => ddl.CourseId.ToString() == courseId);
            this.UpdateDDLInfo(courseId);
            UpdateDDL?.Invoke();
        }
    }
}
